using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;
public partial class admin_A_KhachHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        
        Pager.PageSize = 2;
        Criteria c = new Criteria();
        if (txt_TenNV.Text != null)
        {
            c.add(Condition.AND, tbKhachHangColums.HOTEN, Condition.LIKE,
                txt_TenNV.Text);
        }
        int total = 0;
        tbKhachHangCollection khachhangCollection =
            KHACHHANG_BUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbKhachHangColums.MAKH.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = khachhangCollection;
        Repeater1.DataBind();

        Pager.ItemCount = total;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
                if (cb_Xoa.Checked == true)
                {
                    HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                    int id = int.Parse(hf_id.Value);
                    KhachHang_DTO khachhang = KHACHHANG_BUS.GetByID(id);
                    if (khachhang.HinhAnh != "1.jpeg")
                    {
                        System.IO.File.Delete(Server.MapPath("~/Photos/" + khachhang.HinhAnh));
                        System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + khachhang.HinhAnh));
                    }
                    KHACHHANG_BUS.delete(id);
                }
            }
        }
        catch (Exception ex)
        {

        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
           e.Item.ItemType == ListItemType.Item)
        {
            KhachHang_DTO khachhang = (KhachHang_DTO)e.Item.DataItem;
            Literal  lit_hotenkh = (Literal )e.Item.FindControl("hyp_hotenkh");

            HyperLink  lit_maquyen = (HyperLink )e.Item.FindControl("hyp_maquyen");
            LoaiNguoiDung_DTO nguoidung = LOAINGUOIDUNG.GetByID (khachhang .MaLoai_NguoiDung );
            lit_maquyen.Text = nguoidung.TenLoai_NguoiDung.ToString();
            lit_maquyen.NavigateUrl = "~/admin/A_suakhachhang.aspx?id=" + khachhang.MaLoai_NguoiDung.ToString();

            Literal lit_Mail = (Literal)e.Item.FindControl("lit_Mail");
            Literal lit_diaChi = (Literal)e.Item.FindControl("lit_diaChi");
            Literal lit_dienThoai = (Literal)e.Item.FindControl("lit_dienThoai");
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");

            
          
            img_HinhAnh .ImageUrl  = "~/Photos/Thumb/" + khachhang.HinhAnh ;

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/admin/A_suakhachhang.aspx?id=" + khachhang.MaKH .ToString();

        }
    }
}
